package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.bo.HcxPlatformUserAddBo;
import com.ruoyi.business.bo.HcxPlatformUserQueryBo;
import com.ruoyi.business.bo.HcxPlatformUserEditBo;
import com.ruoyi.business.domain.HcxPlatformUser;
import com.ruoyi.business.mapper.HcxPlatformUserMapper;
import com.ruoyi.business.vo.HcxPlatformUserVo;
import com.ruoyi.business.service.IHcxPlatformUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 平台用户Service业务层处理
 *
 * @author wsdz
 * @date 2023-01-12
 */
@Service
public class HcxPlatformUserServiceImpl extends ServicePlusImpl<HcxPlatformUserMapper, HcxPlatformUser> implements IHcxPlatformUserService {

    @Override
    public HcxPlatformUserVo queryById(Long id){
        return getVoById(id, HcxPlatformUserVo.class);
    }

    @Override
    public TableDataInfo<HcxPlatformUserVo> queryPageList(HcxPlatformUserQueryBo bo) {
        PagePlus<HcxPlatformUser, HcxPlatformUserVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HcxPlatformUserVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<HcxPlatformUserVo> queryList(HcxPlatformUserQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HcxPlatformUserVo.class);
    }

    private LambdaQueryWrapper<HcxPlatformUser> buildQueryWrapper(HcxPlatformUserQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HcxPlatformUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getAliAccount()), HcxPlatformUser::getAliAccount, bo.getAliAccount());
        lqw.eq(StrUtil.isNotBlank(bo.getWxAccount()), HcxPlatformUser::getWxAccount, bo.getWxAccount());
        lqw.like(StrUtil.isNotBlank(bo.getRealName()), HcxPlatformUser::getRealName, bo.getRealName());
        lqw.eq(bo.getBalance() != null, HcxPlatformUser::getBalance, bo.getBalance());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), HcxPlatformUser::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HcxPlatformUserAddBo bo) {
        HcxPlatformUser add = BeanUtil.toBean(bo, HcxPlatformUser.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HcxPlatformUserEditBo bo) {
        HcxPlatformUser update = BeanUtil.toBean(bo, HcxPlatformUser.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HcxPlatformUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importHcxPlatformUser(List<HcxPlatformUserVo> hcxPlatformUserList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(hcxPlatformUserList), "导入数据不能为空");
        int successNum = 0;
        for (HcxPlatformUserVo hcxPlatformUser : hcxPlatformUserList) {
            HcxPlatformUser add = BeanUtil.toBean(hcxPlatformUser, HcxPlatformUser.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", hcxPlatformUserList.size(), successNum);
        if (successNum != hcxPlatformUserList.size()) {
            msg = msg + String.format("失败 %d 条！", hcxPlatformUserList.size() - successNum);
        }
        return msg;
    }
}
